package com.zhouyibike.uu.ui.activity.share;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.ShareCodeResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.ui.base.MyApplication;
import com.zhouyibike.uu.utils.Util;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.OnekeyShareTheme;

public class MyShareMsgActivity2 extends BaseActivity implements View.OnClickListener{
    private ImageButton mBtnBack;
    private ImageView barshare;
    private TextView mBtnTitle;
    private TextView t1, t2, t3, t4, t5,t7,t8,t9,t10,t6;
    private LinearLayout  l2, l3, l4, l5,l7,l8,l9,l10,l6;
    private String shareurl;
    private TextView tv_des,tv_t3;
    private TextView tv_mynumber;
    private String s3="邀请人每成功邀请一人,得一张用车券,被邀请人认购单车后,邀请人获得认购金额<font color='#FF8624'>10%</font>的奖励";
    private int QR_WIDTH; int QR_HEIGHT;
    private ImageView erweima;
    private Button btoneshot;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_share_msg2);
        initView();
        QR_WIDTH= Util.dp2px(this,199);QR_HEIGHT= Util.dp2px(this,199);
        getMsg();
    }
    private void initView() {
        btoneshot= (Button) findViewById(R.id.oneshotshare);
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("邀请分享");
        mBtnBack.setOnClickListener(this);
        btoneshot.setOnClickListener(this);
        erweima= (ImageView) findViewById(R.id.erweima);
    }
    private void getMsg(){
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token",""));
        showProgressDialog();
        addSubscription(apiStores.MyShared(params),
                new ApiCallback<ShareCodeResult>() {

                    @Override
                    public void onSuccess(ShareCodeResult model) {
                        if(model.getStatus()== Config.SUCCESSCODE){
                            shareurl=model.getData().getLink();
                            createQRImage(shareurl);
                            } else{
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
            case R.id.oneshotshare:
                showShare(MyShareMsgActivity2.this,preferences.getString("logo",""),shareurl);
                break;
        }
    }
    /**
     * 演示调用ShareSDK执行分享
     *
     */
    public static void showShare(Context context,String imgurl, String shareurl) {
        OnekeyShare oks = new OnekeyShare();
        oks.setSilent(!true);
//        //ShareSDK快捷分享提供两个界面第一个是九宫格 CLASSIC  第二个是SKYBLUE
        oks.setTheme(OnekeyShareTheme.CLASSIC);
        // 令编辑页面显示为Dialog模式
        oks.setDialogMode();
        // 在自动授权时可以禁用SSO方式
        oks.disableSSOWhenAuthorize();
        oks.setTitle("加入周翼单车");
        oks.setTitleUrl(shareurl);
        oks.setText("朋友们，你我一起享受骑行，赶快下载周翼单车APP");
        oks.setImageUrl(imgurl);
        oks.setUrl(shareurl); //微信不绕过审核分享链接
        oks.setComment("朋友们，你我一起享受骑行，赶快下载周翼单车APP"); //我对这条分享的评论，仅在人人网和QQ空间使用，否则可以不提供
        oks.setSite("加入周翼单车");  //QZone分享完之后返回应用时提示框上显示的名称
        oks.setSiteUrl(shareurl);//QZone分享参数
        oks.setVenueName("加入周翼单车");
        oks.setVenueDescription("朋友们，你我一起享受骑行，赶快下载周翼单车APP");
        oks.setLatitude((float) MyApplication.getInstances().getLat());
        oks.setLongitude((float) MyApplication.getInstances().getLng());
        oks.show(context);
    }
    //要转换的地址或字符串,可以是中文
    public void createQRImage(String url)
    {
        try
        {
            //判断URL合法性
            if (url == null || "".equals(url) || url.length() < 1)
            {
                return;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            //下面这里按照二维码的算法，逐个生成二维码的图片，
            //两个for循环是图片横列扫描的结果
            for (int y = 0; y < QR_HEIGHT; y++)
            {
                for (int x = 0; x < QR_WIDTH; x++)
                {
                    if (bitMatrix.get(x, y))
                    {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    }
                    else
                    {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }
                }
            }
            //生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            //显示到一个ImageView上面
            erweima.setImageBitmap(bitmap);
            erweima.setVisibility(View.VISIBLE);
        }
        catch (WriterException e)
        {
            e.printStackTrace();
        }
    }
}
